package com.training1.core.modules.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.core.common.global.Global;
import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.dto.AccountSystemParamDto;
import com.training1.core.dto.DistributorSettingParamDto;
import com.training1.core.dto.SystemParamDto;
import com.training1.core.dto.SystemTimeSettingParamDto;
import com.training1.core.dto.WellSettingParamDto;
import com.training1.core.entity.SystemParamEntity;
import com.training1.core.modules.dao.SystemSettingsDao;
@Service
public class SystemSettingsService extends CrudService<SystemSettingsDao,SystemParamEntity> {
	
	/**
	 * 分销塔设置保存数据（插入和更新）
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public String saveSettings(SystemParamDto dto) {
		List<SystemParamEntity> entities = convert2Entities(dto);
		String message = Global.getMessage("module.distributorsetting.save.success");
		for(int i=0;i<entities.size();i++){
			SystemParamEntity para =  dao.findEntityByKey(entities.get(i).getParamKey());
			if(para==null||para.getParamKey()==null){
				dao.insert(entities.get(i));
			}else{
				dao.update(entities.get(i));
				message= Global.getMessage("module.distributorsetting.update.success");
			}
		}
		return message;
	}
	/**
	 * 井口设置保存数据（插入和更新）DistributorSettingParamDto
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public String saveWellSettings(WellSettingParamDto dto) {
		System.out.println("services");
		List<SystemParamEntity> entities = convert2WellEntities(dto);
		String message = Global.getMessage("module.distributorsetting.save.success");
		for(int i=0;i<entities.size();i++){
			SystemParamEntity para =  dao.findEntityByKey(entities.get(i).getParamKey());
			if(para==null||para.getParamKey()==null){
				dao.insert(entities.get(i));
			}else{
				dao.update(entities.get(i));
				message= Global.getMessage("module.distributorsetting.update.success");
			}
		}
		return message;
	}
	
	/**
	 * 分销奖励设置设置保存数据（插入和更新）
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public String saveDistributorWellSettings(DistributorSettingParamDto dto) {
		System.out.println("services");
		List<SystemParamEntity> entities = convert2DistributorEntities(dto);
		String message = Global.getMessage("module.distributorsetting.save.success");
		for(int i=0;i<entities.size();i++){
			SystemParamEntity para =  dao.findEntityByKey(entities.get(i).getParamKey());
			if(para==null||para.getParamKey()==null){
				dao.insert(entities.get(i));
			}else{
				dao.update(entities.get(i));
				message= Global.getMessage("module.distributorsetting.update.success");
			}
		}
		return message;
	}
	
	/**
	 * 系统时间设置查询所有数据
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = true)
	public SystemTimeSettingParamDto findTimeAllList(){
		List<SystemParamEntity> list = dao.findAllList();
		return convert2TimeDto(list);
	}
	
	
	/**
	 * 系统时间设置保存数据（插入和更新）
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public String saveTimeSettings(SystemTimeSettingParamDto dto) {
		List<SystemParamEntity> entities = convert2TimeEntities(dto);
		String message = Global.getMessage("module.timesetting.save.success");
		for(int i=0;i<entities.size();i++){
			SystemParamEntity para =  dao.findEntityByKey(entities.get(i).getParamKey());
			if(para==null||para.getParamKey()==null){
				dao.insert(entities.get(i));
			}else{
				dao.update(entities.get(i));
				message= Global.getMessage("module.timesetting.update.success");
			}
		}
		return message;
	}
	
	
	
	/**
	 * 分销塔设置查询所有数据
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = true)
	public SystemParamDto findAllList(){
		List<SystemParamEntity> list = dao.findAllList();
		return convert2Dto(list);
	}
	
	
	/**
	 * 账户设置保存数据（插入和更新）
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public String saveaccountSettings(AccountSystemParamDto dto) {
		List<SystemParamEntity> entities = convert2AccountEntities(dto);
		String message = Global.getMessage("module.accountsetting.save.success");
		for(int i=0;i<entities.size();i++){
			SystemParamEntity para =  dao.findEntityByKey(entities.get(i).getParamKey());
			if(para==null||para.getParamKey()==null){
				dao.insert(entities.get(i));
			}else{
				dao.update(entities.get(i));
				message= Global.getMessage("module.accountsetting.update.success");
			}
		}
		return message;
	}
	
	/**
	 * 账户设置查询所有数据
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = true)
	public AccountSystemParamDto findAccountAllList(){
		List<SystemParamEntity> list = dao.findAllList();
		return convert2AccountDto(list);
	}
	
	
	
	
	private SystemParamDto convert2Dto(List<SystemParamEntity> param) {
		SystemParamDto dto =new SystemParamDto();
		if(param == null || param.size() == 0) {
			return dto;
		}
		// populate the data...
		for(int i=0;i<param.size();i++){
			if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.A_ORDER_CARSH_KEY)){
				dto.setaOrderCash(param.get(i).getParamValue());
				dto.setaOrderCashKey(param.get(i).getParamKey());
			}else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.A_LEVELONE_KEY)){
				dto.setaLevelOneCash(param.get(i).getParamValue());
				dto.setaLevelOneKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.A_LEVELTWO_KEY)){
				dto.setaLevelTwoCash(param.get(i).getParamValue());
				dto.setaLevelTwoKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.A_LEVELTHREE_KEY)){
				dto.setaLevelThreeCash(param.get(i).getParamValue());
				dto.setaLevelThreeKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.A_COMMANDCASH_KEY)){
				dto.setaCommandCash(param.get(i).getParamValue());
				dto.setaCommandCashKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.A_WELLHEADCOUNT_KEY)){
				dto.setaWellheadCount(param.get(i).getParamValue());
				dto.setaWellheadCountKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.B_ORDERCASH_KEY)){
				dto.setbOrderCash(param.get(i).getParamValue());
				dto.setbOrderCashKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.B_LEVELONE_CASH_KEY)){
				dto.setbLevelOneCash(param.get(i).getParamValue());
				dto.setbLevelOneCashKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.B_LEVELTWO_CASH_KEY)){
				dto.setbLevelTwoCash(param.get(i).getParamValue());
				dto.setbLevelTwoCashKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.B_LEVELTHREE_CASH_KEY)){
				dto.setbLevelThreeCash(param.get(i).getParamValue());
				dto.setbLevelThreeCashKey(param.get(i).getParamKey());
			}else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.B_WELLHEADCOUNT_KEY)){
				dto.setbWellheadCount(param.get(i).getParamValue());
				dto.setbWellheadCountKey(param.get(i).getParamKey());
			}else if(StringUtils.equals(param.get(i).getParamKey(), SystemParamDto.FX_KAIDAN_TJ_KEY)){
				dto.setbRecommend(param.get(i).getParamValue());
				dto.setbRecommendKey(param.get(i).getParamKey());
			}
		}
		return dto;
	}

	private List<SystemParamEntity> convert2Entities(SystemParamDto dto){
		List<SystemParamEntity> entities = new ArrayList<SystemParamEntity>();
		SystemParamEntity aorderKvEntity = new SystemParamEntity();
		aorderKvEntity.setParamKey(dto.getaOrderCashKey());
		aorderKvEntity.setParamValue(dto.getaOrderCash());
		entities.add(aorderKvEntity);
		
		SystemParamEntity aLevelOneKVEntity = new SystemParamEntity();
		aLevelOneKVEntity.setParamKey(dto.getaLevelOneKey());
		aLevelOneKVEntity.setParamValue(dto.getaLevelOneCash());
		entities.add(aLevelOneKVEntity);
		
		
		SystemParamEntity aLevelTwoKVEntity = new SystemParamEntity();
		aLevelTwoKVEntity.setParamValue(dto.getaLevelTwoCash());
		aLevelTwoKVEntity.setParamKey(dto.getaLevelTwoKey());
		entities.add(aLevelTwoKVEntity);
		
		
		SystemParamEntity aLevelThreeKVEntity = new SystemParamEntity();
		aLevelThreeKVEntity.setParamValue(dto.getaLevelThreeCash());
		aLevelThreeKVEntity.setParamKey(dto.getaLevelThreeKey());
		entities.add(aLevelThreeKVEntity);
		
		
		SystemParamEntity aCommandKVEntity = new SystemParamEntity();
		aCommandKVEntity.setParamValue(dto.getaCommandCash());
		aCommandKVEntity.setParamKey(dto.getaCommandCashKey());
		entities.add(aCommandKVEntity);
		
		
		SystemParamEntity aWellheadCountKVEntity = new SystemParamEntity();
		aWellheadCountKVEntity.setParamValue(dto.getaWellheadCount());
		aWellheadCountKVEntity.setParamKey(dto.getaWellheadCountKey());
		entities.add(aWellheadCountKVEntity);
		
		
		SystemParamEntity borderKvEntity = new SystemParamEntity();
		borderKvEntity.setParamValue(dto.getbOrderCash());
		borderKvEntity.setParamKey(dto.getbOrderCashKey());
		entities.add(borderKvEntity);
		
		
		SystemParamEntity bLevelOneKVEntity = new SystemParamEntity();
		bLevelOneKVEntity.setParamValue(dto.getbLevelOneCash());
		bLevelOneKVEntity.setParamKey(dto.getbLevelOneCashKey());
		entities.add(bLevelOneKVEntity);
		
		
		SystemParamEntity bLevelTwoKVEntity = new SystemParamEntity();
		bLevelTwoKVEntity.setParamValue(dto.getbLevelTwoCash());
		bLevelTwoKVEntity.setParamKey(dto.getbLevelTwoCashKey());
		entities.add(bLevelTwoKVEntity);
		
		
		SystemParamEntity bLevelThreeKVEntity = new SystemParamEntity();
		bLevelThreeKVEntity.setParamValue(dto.getbLevelThreeCash());
		bLevelThreeKVEntity.setParamKey(dto.getbLevelThreeCashKey());
		entities.add(bLevelThreeKVEntity);
		
		
		SystemParamEntity bWellheadCountKVEntity = new SystemParamEntity();
		bWellheadCountKVEntity.setParamValue(dto.getbWellheadCount());
		bWellheadCountKVEntity.setParamKey(dto.getbWellheadCountKey());
		entities.add(bWellheadCountKVEntity);
		
		SystemParamEntity bRcommendEntity = new SystemParamEntity();
		bRcommendEntity.setParamValue(dto.getbRecommend());
		bRcommendEntity.setParamKey(dto.getbRecommendKey());
		entities.add(bRcommendEntity);

		return entities;
	}
	
	private List<SystemParamEntity> convert2DistributorEntities(DistributorSettingParamDto dto){
		List<SystemParamEntity> entities = new ArrayList<SystemParamEntity>();
		SystemParamEntity aorderKvEntity = new SystemParamEntity();
		aorderKvEntity.setParamKey(dto.getaOrderCashKey());
		aorderKvEntity.setParamValue(dto.getaOrderCash());
		entities.add(aorderKvEntity);
		
		SystemParamEntity aLevelOneKVEntity = new SystemParamEntity();
		aLevelOneKVEntity.setParamKey(dto.getaLevelOneKey());
		aLevelOneKVEntity.setParamValue(dto.getaLevelOneCash());
		entities.add(aLevelOneKVEntity);
		
		
		SystemParamEntity aLevelTwoKVEntity = new SystemParamEntity();
		aLevelTwoKVEntity.setParamValue(dto.getaLevelTwoCash());
		aLevelTwoKVEntity.setParamKey(dto.getaLevelTwoKey());
		entities.add(aLevelTwoKVEntity);
		
		
		SystemParamEntity aLevelThreeKVEntity = new SystemParamEntity();
		aLevelThreeKVEntity.setParamValue(dto.getaLevelThreeCash());
		aLevelThreeKVEntity.setParamKey(dto.getaLevelThreeKey());
		entities.add(aLevelThreeKVEntity);
		
		
		SystemParamEntity aCommandKVEntity = new SystemParamEntity();
		aCommandKVEntity.setParamValue(dto.getaCommandCash());
		aCommandKVEntity.setParamKey(dto.getaCommandCashKey());
		entities.add(aCommandKVEntity);
		
		
		SystemParamEntity borderKvEntity = new SystemParamEntity();
		borderKvEntity.setParamValue(dto.getbOrderCash());
		borderKvEntity.setParamKey(dto.getbOrderCashKey());
		entities.add(borderKvEntity);
		
		
		SystemParamEntity bLevelOneKVEntity = new SystemParamEntity();
		bLevelOneKVEntity.setParamValue(dto.getbLevelOneCash());
		bLevelOneKVEntity.setParamKey(dto.getbLevelOneCashKey());
		entities.add(bLevelOneKVEntity);
		
		
		SystemParamEntity bLevelTwoKVEntity = new SystemParamEntity();
		bLevelTwoKVEntity.setParamValue(dto.getbLevelTwoCash());
		bLevelTwoKVEntity.setParamKey(dto.getbLevelTwoCashKey());
		entities.add(bLevelTwoKVEntity);
		
		
		SystemParamEntity bLevelThreeKVEntity = new SystemParamEntity();
		bLevelThreeKVEntity.setParamValue(dto.getbLevelThreeCash());
		bLevelThreeKVEntity.setParamKey(dto.getbLevelThreeCashKey());
		entities.add(bLevelThreeKVEntity);
		
		SystemParamEntity bRcommendEntity = new SystemParamEntity();
		bRcommendEntity.setParamValue(dto.getbRecommend());
		bRcommendEntity.setParamKey(dto.getbRecommendKey());
		entities.add(bRcommendEntity);
		
		return entities;
	}
	
	private List<SystemParamEntity> convert2AccountEntities(AccountSystemParamDto dto){
		List<SystemParamEntity> entities = new ArrayList<SystemParamEntity>();
		
		SystemParamEntity incomeTaxRateKVEntity = new SystemParamEntity();
		incomeTaxRateKVEntity.setParamValue(dto.getIncomeTaxRate());
		incomeTaxRateKVEntity.setParamKey(dto.getIncomeTaxRateKey());
		entities.add(incomeTaxRateKVEntity);
		
		SystemParamEntity counsumeFundAccountingKVEntity = new SystemParamEntity();
		counsumeFundAccountingKVEntity.setParamValue(dto.getCounsumeFundAccounting());
		counsumeFundAccountingKVEntity.setParamKey(dto.getCounsumeFundAccountingKey());
		entities.add(counsumeFundAccountingKVEntity);
		
		SystemParamEntity agentsFightDeductionKVEntity = new SystemParamEntity();
		agentsFightDeductionKVEntity.setParamValue(dto.getAgentsFightDeduction());
		agentsFightDeductionKVEntity.setParamKey(dto.getAgentsFightDeductionKey());
		entities.add(agentsFightDeductionKVEntity);
		
		return entities;
		
	}
	
	
	private AccountSystemParamDto convert2AccountDto(List<SystemParamEntity> param) {
		AccountSystemParamDto dto =new AccountSystemParamDto();
		if(param == null || param.size() == 0) {
			return dto;
		}
		
		// populate the data...
		for(int i=0;i<param.size();i++){
			if(StringUtils.equals(param.get(i).getParamKey(), AccountSystemParamDto.B_INCOME_TAX_RATE_KEY)){
				dto.setIncomeTaxRate(param.get(i).getParamValue());
				dto.setIncomeTaxRateKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), AccountSystemParamDto.B_CONSUMER_FUND_ACCOUNTING_KEY)){
				dto.setCounsumeFundAccounting(param.get(i).getParamValue());
				dto.setCounsumeFundAccountingKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(), AccountSystemParamDto.AGENTS_FIGHT_DEDUCTION_KEY)){
				dto.setAgentsFightDeduction(param.get(i).getParamValue());
				dto.setAgentsFightDeductionKey(param.get(i).getParamKey());
			}
		}
		
		return dto;

	}	
	
	
	private SystemTimeSettingParamDto convert2TimeDto(List<SystemParamEntity> param) {
		SystemTimeSettingParamDto dto =new SystemTimeSettingParamDto();
		if(param == null || param.size() == 0) {
			return dto;
		}
		
		// populate the data...
		for(int i=0;i<param.size();i++){
			if(StringUtils.equals(param.get(i).getParamKey(),SystemTimeSettingParamDto.SYSTEM_LOGIN_BEGIN_TIME_KEY)){
				dto.setLoginBeginTime(param.get(i).getParamValue());
				dto.setLoginBeginTimeKey(param.get(i).getParamKey());
			}
			
			else if(StringUtils.equals(param.get(i).getParamKey(),SystemTimeSettingParamDto.SYSTEM_LOGIN_END_TIME_KEY)){
				dto.setLoginEndTime(param.get(i).getParamValue());
				dto.setLoginEndTimeKey(param.get(i).getParamKey());
			}
			else if(StringUtils.equals(param.get(i).getParamKey(),SystemTimeSettingParamDto.CLIENT_DATE)){
				dto.setClientDate(param.get(i).getParamValue());
				dto.setClientDateKey(param.get(i).getParamKey());
			}
		}						
		return dto;
	}
	
	
	
	private List<SystemParamEntity> convert2TimeEntities(SystemTimeSettingParamDto dto){
		List<SystemParamEntity> entities = new ArrayList<SystemParamEntity>();
		SystemParamEntity loginBeginTimeKVEntity = new SystemParamEntity();
		loginBeginTimeKVEntity.setParamValue(dto.getLoginBeginTime());
		loginBeginTimeKVEntity.setParamKey(dto.getLoginBeginTimeKey());
		entities.add(loginBeginTimeKVEntity);
		
		SystemParamEntity loginEndTimeKVEntity = new SystemParamEntity();
		loginEndTimeKVEntity.setParamValue(dto.getLoginEndTime());
		loginEndTimeKVEntity.setParamKey(dto.getLoginEndTimeKey());
		entities.add(loginEndTimeKVEntity);
		
		SystemParamEntity loginClientDateKVEntity = new SystemParamEntity();
		loginClientDateKVEntity.setParamValue(dto.getClientDate());
		loginClientDateKVEntity.setParamKey(dto.getClientDateKey());
		entities.add(loginClientDateKVEntity);
		
		return entities;
		
	}
	
	private List<SystemParamEntity> convert2WellEntities(WellSettingParamDto dto){
		List<SystemParamEntity> entities = new ArrayList<SystemParamEntity>();
		SystemParamEntity aWellheadCountKVEntity = new SystemParamEntity();
		aWellheadCountKVEntity.setParamValue(dto.getaWellheadCount());
		aWellheadCountKVEntity.setParamKey(dto.getaWellheadCountKey());
		entities.add(aWellheadCountKVEntity);
		
		SystemParamEntity bWellheadCountKVEntity = new SystemParamEntity();
		bWellheadCountKVEntity.setParamValue(dto.getbWellheadCount());
		bWellheadCountKVEntity.setParamKey(dto.getbWellheadCountKey());
		entities.add(bWellheadCountKVEntity);

		return entities;
		
	}
	
	public SystemParamEntity findEntityByKey(String key){
		return dao.findEntityByKey(key);
	}
	
}
